package com.java.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.BsCourse;
import com.java.entity.BsPaper;
import com.java.entity.BsPaperItem;
import com.java.entity.ext.BsPaperVo;
import com.java.mapper.BsCourseMapper;
import com.java.mapper.BsPaperItemMapper;
import com.java.mapper.BsPaperMapper;
import com.java.service.BsPaperService;
import com.java.util.MyMap;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;


@Service
public class BsPaperServiceImpl implements BsPaperService {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired private BsPaperMapper paperMapper;
    @Autowired private BsPaperItemMapper itemMapper;
    @Autowired private BsCourseMapper courseMapper;
    
	@Override
	public TableRespone getPapers(String title, String typeId, int page, int size) {
		MyMap mm = new MyMap()
				.put("paperTitle", title)
				.put("paperCourseId", typeId)
				.put("paperDelete", 0);
		int offset = TableRespone.getOffset(page, size);
		List<BsPaper> list = paperMapper.selectAll(mm, offset, size);
		List<BsPaper> count = paperMapper.selectAll(mm, 0, 0);
		List<BsPaperVo> clist = new ArrayList<BsPaperVo>();
		if(count.size() > 0) {
			for (BsPaper bsPaper : list) {
				BsCourse c = courseMapper.selectByKey(bsPaper.getPaperCourseId());
				clist.add(new BsPaperVo(bsPaper, c));
			}
		}
		return new TableRespone(clist, count.size());
	}
	
	@Override
	public BaseRespone savePaper(BsPaper paper) {
		int row = 0;
		BsPaper info = paperMapper.selectByKey(paper.getPaperId());
		if(null == info) {
			paper.setPaperId(MyTools.getUUID());
			paper.setPaperTime(new Date());
			paper.setPaperDelete(0);
			row = paperMapper.insertSelective(paper);
		}else {
			row = paperMapper.updateSelective(paper);
		}
		if(row > 0) {
			return BaseRespone.success("保存成功");
		}
		return BaseRespone.error("保存失败");
	}
	
	@Override
	public BaseRespone delPaper(String paperId) {
		int row = paperMapper.deleteUpdate(paperId, "paper_delete");
		if(row > 0) {
			return BaseRespone.success("删除成功");
		}
		return BaseRespone.error("删除失败");
	}
	
	@Override
	public TableRespone getItems(String paperId) {
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("itemPaperId", paperId);
		mm.put("itemDelete", 0);
		List<BsPaperItem> list = itemMapper.selectAllNotPage(mm);
		return new TableRespone(list, list.size());
	}
	
	@Override
	public BaseRespone saveItem(BsPaperItem item) {
		int row = 0;
		BsPaperItem info = itemMapper.selectByKey(item.getItemId());
		if(null == info) {
			item.setItemId(MyTools.getUUID());
			item.setItemTime(new Date());
			item.setItemDelete(0);
			row = itemMapper.insertSelective(item);
		}else {
			row = itemMapper.updateSelective(item);
		}
		if(row > 0) {
			return BaseRespone.success("保存成功");
		}
		return BaseRespone.error("保存失败");
	}
	
	@Override
	public BaseRespone delItem(String itemId) {
		int row = itemMapper.deleteUpdate(itemId, "item_delete");
		if(row > 0) {
			return BaseRespone.success("删除成功");
		}
		return BaseRespone.error("删除失败");
	}
    
}
